package comits.web.client.servlets.demo;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import comits.business.tiers.dao.FrontiereDAO;
import comits.business.tiers.dao.FrontiereDAOImpl;
import comits.business.tiers.dao.ProduitStockeDAO;
import comits.business.tiers.dao.ProduitStockeDAOImpl;
import comits.business.tiers.metiers.ListProduitVentes;
import comits.business.tiers.metiers.ProduitStocke;
import comits.business.tiers.rp.tasks.ws.ControleurClassification;
import comits.business.tiers.utilitaires.Dates;

/**
 * 
 * @author Dourga
 */
public class ClassificationServlet extends HttpServlet {
	
	private static final long serialVersionUID = 7526928611471348888L;
	

	public void init() throws ServletException {
	}

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void doGet(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		
		}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(); 
		
		
		int mois_deb =  Integer.parseInt(request.getParameter("MoisDebut"));
		int annee_deb =  Integer.parseInt(request.getParameter("AnneeDebut"));
		int  mois_fin =  Integer.parseInt(request.getParameter("MoisFin"));
		int annee_fin=Integer.parseInt(request.getParameter("AnneeFin")); 
		Dates debut= new Dates();
		Dates fin = new Dates();
		
		debut = debut.setDates(annee_deb, mois_deb, 1);
		fin = fin.setDates(annee_fin, mois_fin, 1);
		
		 int A =  Integer.parseInt(request.getParameter("AB"));
	        int B =  Integer.parseInt(request.getParameter("BC"));
	        int Bb = B+A;
		
		
		 if (annee_deb>annee_fin )
         {
                 getServletContext().getRequestDispatcher("/AfficherClassification.jsp").forward(request, response);
         
                                 
         }
         else if (annee_deb==annee_fin && mois_deb>=mois_fin )
         {
                 getServletContext().getRequestDispatcher("/AfficherClassification.jsp").forward(request, response);
                 //o=1;
                 //session.setAttribute("o", o);
         
         }
         else if (Bb>=100 )
         {
                 getServletContext().getRequestDispatcher("/AfficherClassification.jsp").forward(request, response);
                 //o=1;
                 //session.setAttribute("o", o);
         
         }
 
                 else{
				
		
		session.removeAttribute("listpV");      
        
        //modif frontiere
       

        FrontiereDAO front = new FrontiereDAOImpl();
        
        session.setAttribute("A",A);
        
        session.setAttribute("B",B);
        /*  
        front.ModifierFrontiereA(A);
        front.ModifierFrontiereB(Bb);*/
		
		ControleurClassification ContClassif = new ControleurClassification();
		
		ContClassif.ModifierFrontiereA(A);
		ContClassif.ModifierFrontiereB(Bb);
		
		List<ListProduitVentes> listpV =  new ArrayList<ListProduitVentes>();
		List<ListProduitVentes> listpV1 =  new ArrayList<ListProduitVentes>();
		listpV = ContClassif.recupererProduit(debut, fin);
		listpV1 = ContClassif.calculMoyenne(listpV);
		listpV1 = ContClassif.OrdreDecroissant(listpV1);
		listpV1 = ContClassif.calculCumulPourcentage(listpV1);
        float[] peri = ContClassif.DeterminerPerimetreFront(listpV1, A, Bb);
		//float[] peri = ContClassif.DeterminerPerimetre(listpV1);
		ContClassif.affecterclasse(listpV, peri);
		
		
		session.setAttribute("listpV",listpV);
		session.setAttribute("listdemo", listpV1);
		session.setAttribute("perimetre1", peri[0]);
		session.setAttribute("perimetre2", peri[1]);
		session.setAttribute("perimetre3", peri[2]);
		getServletContext().getRequestDispatcher("/ResultatClassification.jsp").forward(request, response);
        }
		
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>
}
